﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TBM.Tools
{
    /// <summary>
    /// URL处理
    /// </summary>
    public class CURL
    {
        string _baseUrl = "";
        Dictionary<string, string> _params = new Dictionary<string, string>();

        public CURL(string url)
        {
            string[] tmp = url.Split('?');
            string queryString = null;

            if (tmp.Length == 1)
            {
                queryString = tmp[0];
            }
            else if (tmp.Length == 2)
            {
                queryString = tmp[1];
                _baseUrl = tmp[0];
            }
            else
            {
                throw new Exception("传入的不是一个合法的URL.");
            }

            foreach (string item in queryString.Split('&'))
            {
                string[] keyvalue = item.Split('=');
                string key = null;
                string value = null;

                if (keyvalue.Length != 2) continue;

                key = keyvalue[0];
                value = keyvalue[1];

                AppendParam(key, value);
            }
        }


        /// <summary>
        /// 追加参数
        /// </summary>
        /// <param name="name">参数名</param>
        /// <param name="value">参数值</param>
        public void AppendParam(string name, string value)
        {
            string __t = "";

            if (_params.TryGetValue(name, out __t))
            {
                _params[name] = value;
            }
            else
            {
                _params.Add(name, value);
            }
        }


        /// <summary>
        /// 添加参数并返回URL（不改变原URL）
        /// </summary>
        /// <param name="name">参数名</param>
        /// <param name="value">参数值</param>
        /// <returns></returns>
        public string GetURLByAddParam(string name, string value)
        {
            bool isFind = false;
            StringBuilder newUrl = new StringBuilder(_baseUrl);

            if (_params.Count > 0)
            {
                newUrl.Append("?");

                foreach (var item in _params)
                {
                    string n = item.Key;
                    string v = item.Value;

                    if (!isFind && n == name)
                    {
                        v = value;
                    }

                    newUrl.AppendFormat("{0}={1}&", n, v);
                }

                if (newUrl[newUrl.Length - 1] == '&')
                {
                    newUrl.Remove(newUrl.Length - 1, 1);
                }
            }

            return newUrl.ToString();
        }


        public static string GetURLByAddParam(string url, string name, string value)
        {
            StringBuilder newUrl = new StringBuilder();
            int index = url.IndexOf('#');

            if (index > -1)
            {
                newUrl.Append(url.Substring(0, index));
            }
            else
            {
                newUrl.Append(url);
            }

            if (url.IndexOf('?') == -1)
            {
                newUrl.Append("?");
            }
            else
            {
                newUrl.Append("&");
            }

            newUrl.AppendFormat("{0}={1}", name, value);

            if (index > -1)
            {
                newUrl.Append(url.Substring(index));
            }

            return newUrl.ToString();
        }


        /// <summary>
        /// 获取地址
        /// </summary>
        /// <returns></returns>
        public string GetURL()
        {
            StringBuilder newUrl = new StringBuilder(_baseUrl);

            if (_params.Count > 0)
            {
                newUrl.Append("?");

                foreach (var item in _params)
                {
                    newUrl.AppendFormat("{0}={1}&", item.Key, item.Value);
                }

                if (newUrl[newUrl.Length - 1] == '&')
                {
                    newUrl.Remove(newUrl.Length - 1, 1);
                }
            }

            return newUrl.ToString();
        }
    }
}
